using System;
using System.Collections.Generic;

using ManageSystemServer.Entity;
using ManageSystemServer.Service;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ManageSystemServer.Controllers
{
    [Route("student")]
    public class StudentController : Controller
    {
        //依赖注入
        private readonly IServiceProvider _services;

        private readonly StudentService _studentService;

        public StudentController(IServiceProvider services)
        {
            _services = services;
            _studentService = _services.GetRequiredService<StudentService>();
        }

        // GET student/:id
        [HttpGet("{id}")]
        public ActionResult<Student?> GetStudentById(int id)
        {
            return _studentService.GetStudentById(id);
        }

        //GET student/all
        [HttpGet("all")]
        public ActionResult<List<Student>> GetAllStudent()
        {
            return _studentService.GetAllStudent();
        }

        //GET student/?name=
        [HttpGet]
        public ActionResult<List<Student>> GetStudentByName(string name)
        {
            return _studentService.GetStudentByName(name);
        }
        //GET student/?major=
        [HttpGet]
        public ActionResult<List<Student>> GetStudentByMajor(string major)
        {
            return _studentService.GetStudentByMajor(major);
        }

        // POST student/add
        [HttpPost("add")]
        public ActionResult<bool> AddStudent([FromBody] Student student)
        {
            if (student == null)
            {
                return new BadRequestResult();
            }
            return _studentService.AddStudent(student);
        }

        // PUT student/reset/:id
        [HttpPut("reset/{id}")]
        public ActionResult<bool> ResetStudent(int id, [FromBody] Student student)
        {
            if (student == null)
                return new BadRequestResult();
            _studentService.ResetStudent(id, student);
            return true;
        }

        // DELETE api/<controller>/5
        [HttpDelete("delete/{id}")]
        public void Delete(int id)
        {
            _studentService.DeleteStudent(id);
        }
    }
}
